import { createRouter, createWebHashHistory } from "vue-router";
import { ExamRoutes } from "./exam";
import { students } from "./students";
import traManger from "./traManger"
import dapaoRouter from "./dapao-router";
import system from "./system";
import auth from "./auth";
import { useUser } from "../store/user";

// 路由汇总
export const MainRoutes = [
    ...dapaoRouter, ...students, ...ExamRoutes, ...traManger, ...system, ...auth
]

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect:'/dashboard/wroktable',
            component: () => import('../layout/index.vue'),
            children: MainRoutes,

        },
        {
            path: '/login',
            component: () => import("../views/login/login.vue"),
        },
        {
            path: '/permit',
            component: () => import("../views/Permit.vue"),
        },
    ]
})

router.beforeEach((to, _, next) => {
    let whiteList = ['/login', '/permit'];
    if (!whiteList.includes(to.path)) {
        const user = useUser()
        if (user.userInfo) {
            let { permission } = user.userInfo!.data
            permission.push('/')
            if (permission.includes(to.path)) {
                next();//有权限
            } else {
                next("/permit")
            }
        } else {
            next("/login")
        }
    } else {
        next()
    }
})


export default router
